# -*- coding: utf-8 -*-
from odoo import http
from odoo.http import request
import io
import xlwt
import simplejson


class AccountReportPrint(http.Controller):

    @http.route('/web/report/designer_export_excel', type='http', auth="user")
    def report_export_excel(self,**data):
        id = simplejson.loads(data['options'])
        report = request.env['account.report.format.designer'].browse([id])
        if report.report_type == 'balance':
           return report.balance_report_out_to_excel(report.balance_sheet_ids)
        if report.report_type == 'income':
            return report.income_report_out_to_excel(report.income_statement_ids)
        if report.report_type == 'cash':
            return report.cash_report_out_to_excel(report.cash_flows_ids)
        if report.report_type in ['tax','tax_detail']:
            return report.tax_report_out_to_excel(report.tax_detail_ids)

    def balance_report_out_to_excel(self,balance_sheet_ids):
        """生成资产负债表的excel"""
        book = xlwt.Workbook(encoding='utf-8', style_compression=0)
        fp = io.BytesIO()
        sheet1 = book.add_sheet('资产负债表')
        style = xlwt.easyxf(
            'font: height 150;'
            'alignment: vert center, horizontal center;'
            'borders: left thin, right thin, top thin, bottom thin;'
        )

        header_style = xlwt.easyxf(
            'font: height 150;'
            'borders: left thin, right thin, top thin, bottom thin;'
            'align: vertical center, horizontal center;'
        )
        # 第一行标题
        sheet1.write_merge(0, 0, 0, 6, '%s资产负债报表'%self.period_id.name, style=header_style)
        #第二行
        sheet1_header = ['资产', '期末余额', '年初余额', '负债及权益类', '期末余额', '年初余额']
        [sheet1.write(1, num, line, header_style) for num, line in enumerate(sheet1_header)]
        for num, line in enumerate(balance_sheet_ids):
            sheet1.write(num + 2, 0, line.asset_name, style)
            sheet1.write(num + 2, 1, line.asset_begin, style)
            sheet1.write(num + 2, 2, line.asset_balance, style)
            sheet1.write(num + 2, 3, line.liability_name, style)
            sheet1.write(num + 2, 4, line.liability_begin, style)
            sheet1.write(num + 2, 5, line.liability_balance, style)
        book.save(fp)
        fp.seek(0)
        content = fp.read()
        return request.make_response(content, headers=[
            ('Content-Type', 'application/vnd.ms-excel'),
            ('Content-Disposition', 'attachment; filename=资产负债报表.xls;')
        ])

    def income_report_out_to_excel(self,income_statement_ids):
        """生成利润表"""
        book = xlwt.Workbook(encoding='utf-8', style_compression=0)
        fp = io.BytesIO()
        sheet1 = book.add_sheet('利润表')
        style = xlwt.easyxf(
            'font: height 150;'
            'alignment: vert center, horizontal center;'
            'borders: left thin, right thin, top thin, bottom thin;'
        )

        header_style = xlwt.easyxf(
            'font: height 150;'
            'borders: left thin, right thin, top thin, bottom thin;'
            'align: vertical center, horizontal center;'
        )
        # 第一行标题
        sheet1.write_merge(0, 0, 0, 7, '%s利润表' % self.period_id.name, style=header_style)
        # 第二行
        sheet1_header = ['项次', '项目', '本年累计数', '本月数', '税别项目', '期末账面数','年初账面数']
        [sheet1.write(1, num, line, header_style) for num, line in enumerate(sheet1_header)]
        for line in income_statement_ids:
            sheet1.write(int(line.print_sequence) + 2, 0, line.sequence, style)
            sheet1.write(int(line.print_sequence) + 2, 1, line.name, style)
            sheet1.write(int(line.print_sequence) + 2, 2, line.year_balance, style)
            sheet1.write(int(line.print_sequence) + 2, 3, line.balance, style)
            sheet1.write(int(line.print_sequence) + 2, 4, line.tax_name, style)
            sheet1.write(int(line.print_sequence) + 2, 5, line.tax_month, style)
            sheet1.write(int(line.print_sequence) + 2, 6, line.tax_year, style)
        book.save(fp)
        fp.seek(0)
        content = fp.read()
        return request.make_response(content, headers=[
            ('Content-Type', 'application/vnd.ms-excel'),
            ('Content-Disposition', 'attachment; filename=资产负债表.xls;')
        ])

    def cash_report_out_to_excel(self,cash_flows_ids):
        """生成现金流量表"""
        book = xlwt.Workbook(encoding='utf-8', style_compression=0)
        sheet1 = book.add_sheet('现金流量表')
        fp = io.BytesIO()
        style = xlwt.easyxf(
            'font: height 150;'
            'alignment: vert center, horizontal center;'
            'borders: left thin, right thin, top thin, bottom thin;'
        )

        header_style = xlwt.easyxf(
            'font: height 150;'
            'borders: left thin, right thin, top thin, bottom thin;'
            'align: vertical center, horizontal center;'
        )
        # 第一行标题
        sheet1.write_merge(0, 0, 0, 7, '%s现金流量表' % self.period_id.name, style=header_style)
        # 第二行
        sheet1_header = ['项次', '项目',  '本年累计数','本月数']
        [sheet1.write(1, num, line, header_style) for num, line in enumerate(sheet1_header)]
        for line in cash_flows_ids:
            sheet1.write(int(line.print_sequence) + 2, 0, line.sequence, style)
            sheet1.write(int(line.print_sequence) + 2, 1, line.name, style)
            sheet1.write(int(line.print_sequence) + 2, 2, line.year_balance, style)
            sheet1.write(int(line.print_sequence) + 2, 3, line.balance, style)
        book.save(sheet1)
        fp.seek(0)
        content = fp.read()
        return request.make_response(content, headers=[
            ('Content-Type', 'application/vnd.ms-excel'),
            ('Content-Disposition', 'attachment; filename=现金流量表.xls;')
        ])

    def tax_report_out_to_excel(self,tax_detail_ids):
        """生成税金表"""
        book = xlwt.Workbook(encoding='utf-8', style_compression=0)
        fp = io.BytesIO()
        if self.report_type == 'tax':
            sheet_name = '税金表'
            filename = '税金表'
        else:
            sheet_name = '应交税金明细表'
            filename = '应交税金明细表'
        sheet1 = book.add_sheet(sheet_name)
        style = xlwt.easyxf(
            'font: height 150;'
            'alignment: vert center, horizontal center;'
            'borders: left thin, right thin, top thin, bottom thin;'
        )

        header_style = xlwt.easyxf(
            'font: height 150;'
            'borders: left thin, right thin, top thin, bottom thin;'
            'align: vertical center, horizontal center;'
        )
        # 第一行标题
        sheet1.write_merge(0, 0, 0, 7, '%s%s' % (self.period_id.name,sheet_name), style=header_style)
        # 第二行
        sheet1_header = ['项次', '项目',  '本年累计数','本月数']
        [sheet1.write(1, num, line, header_style) for num, line in enumerate(sheet1_header)]
        for line in tax_detail_ids:
            sheet1.write(int(line.print_sequence) + 2, 0, line.sequence, style)
            sheet1.write(int(line.print_sequence) + 2, 1, line.name, style)
            sheet1.write(int(line.print_sequence) + 2, 2, line.year_balance, style)
            sheet1.write(int(line.print_sequence) + 2, 3, line.balance, style)
        book.save(fp)
        fp.seek(0)
        content = fp.read()

        return request.make_response(content, headers=[
            ('Content-Type', 'application/vnd.ms-excel'),
            ('Content-Disposition', 'attachment; filename=%s.xls;' % filename)
        ])
